package com.jiazhong.Service.Impl;

import com.jiazhong.Dao.Impl.UserDaoImpl;
import com.jiazhong.Dao.UserDao;
import com.jiazhong.Model.User;
import com.jiazhong.Service.UserService;
import com.jiazhong.exceptions.BadPasswordException;
import com.jiazhong.exceptions.UsernameNotFoundException;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    @Override
    public User login(String usernmae, String password) throws UsernameNotFoundException, BadPasswordException {
        //根据用户名获得用户信息
        User user = userDao.loadUserByUsername(usernmae);
        //判断用户名是否存在
        if(user == null){//用户名不存在
            throw new UsernameNotFoundException();
        }
        //判断密码是否正确
        if(!password.equals(user.getUser_password())){
            throw new BadPasswordException();//密码错误
        }
        //设置user对象中的密码为null
        user.setUser_password(null);
        return user;
    }

    @Override
    public void register(User user) {
        userDao.addUser(user);
    }
}
